#include "print.h"
#include <QtPrintSupport/QPrinter>
#include <QPainter>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <qguiapplication.h>

Print::Print(QObject *parent) : QObject(parent)
{

}

Print::~Print()
{
}

QStringList Print::availablePrinterNames() const
{
    return QPrinterInfo::availablePrinterNames();
}

QString Print::defaultPrinterName() const
{
    return QPrinterInfo::defaultPrinterName();
}

void Print::printTrade(const QString &printName, const QVariantMap &jsonData)
{
#ifdef Q_OS_WIN
    QPrinter printer(this->printerInfo(printName), QPrinter::HighResolution);

    printer.setPaperSize( QSizeF(241.0, 140.0), QPrinter::Millimeter);
    printer.setPageMargins(QMarginsF(3, 3, 3, 3), QPageLayout::Millimeter);

    const qreal MM = printer.logicalDpiY() * 10.0 / 254;


    QVariantList array = jsonData.value("data").toList();

    const int count  = 5;

    QMutableListIterator<QVariant> itor(array);
    QVariantList list;
    qreal y = 0.0;

    QPainter painter(&printer);

    while (itor.hasNext()) {
        do {
            list << itor.next();
            itor.remove();
            if (list.count() == count)
                break;
        } while(itor.hasNext());

        y = printHeader(MM, &painter,  jsonData.value("header").toMap());
        y = printContent(y, MM, list, &painter);

        list.clear();

        if (itor.hasNext())
            printer.newPage();
    }

    printFooter(y, MM, &painter, jsonData.value("footer").toMap());

#endif
}


QPrinterInfo Print::printerInfo(const QString &printName)
{
    QList<QPrinterInfo>	printers = QPrinterInfo::availablePrinters();

    foreach (QPrinterInfo p, printers) {
        if(p.printerName() == printName) {
            return p;
        }
    }

    return QPrinterInfo::defaultPrinter();
}

qreal Print::printHeader(qreal mm, QPainter *painter, const QVariantMap &header)
{
    QPrinter *printer = dynamic_cast<QPrinter *>(painter->device());

    QMarginsF margins = printer->pageLayout().margins();
    qreal width = printer->paperSize(QPrinter::Millimeter).width() - margins.left() - margins.right();

    QPointF point(margins.left() * mm, margins.top() * mm);
    // LOGO
    {
        QRectF rectangle;
        rectangle.setX(point.x());
        rectangle.setY(point.y());
        rectangle.setWidth(32 *mm);
        rectangle.setHeight(32 * mm);
        QPixmap pixmap(":/favicon.png");
        painter->drawPixmap(rectangle, pixmap, pixmap.rect());
    }

    // 支付宝
    {
        QRectF rectangle;
        rectangle.setX((width - 64)*mm);
        rectangle.setY(point.y());
        rectangle.setWidth(32 *mm);
        rectangle.setHeight(32 * mm);
        QPixmap pixmap(":/zfb.png");
        painter->drawPixmap(rectangle, pixmap, pixmap.rect());
    }

    // 微信
    {
        QRectF rectangle;
        rectangle.setX((width - 98)*mm);
        rectangle.setY(point.y());
        rectangle.setWidth(32 *mm);
        rectangle.setHeight(32 * mm);
        QPixmap pixmap(":/wx.png");
        painter->drawPixmap(rectangle, pixmap, pixmap.rect());
    }

    QFont font;
    font.setPointSize(11.0);
    painter->setFont(font);

    float height = 6.0 * mm;

    float y = point.y() + height * 2 ;
    qreal x = point.x();

    // 网址
    painter->drawText(x + 42 * mm, y, QByteArray::fromBase64("572R5Z2AOiA=") + header.value("url").toString());

    // 地址
    y +=height;
    painter->drawText(x + 42 * mm, y, QByteArray::fromBase64("5Zyw5Z2AOiA=") + header.value("address").toString());

    // 电话
    y +=height;
    painter->drawText(x + 42 * mm, y, QByteArray::fromBase64("55S16K+dOiA=") + header.value("tel").toString());

    // 联系人
    y +=height;
    painter->drawText(x + 42 * mm, y, "联系人：" + header.value("contacts").toString());

    y +=height * 1.2;

    x = x + 42 * mm;
    painter->setPen(Qt::DotLine);
    painter->drawLine(x, y, width * mm - margins.right() * mm, y);

    x = point.x();

    // 单号
    y += height;
    painter->drawText(x + 24 * mm, y, QByteArray::fromBase64("5Y2V5Y+377ya") + header.value("tid").toString());

    // 日期
    painter->drawText(x + 120 * mm, y, QByteArray::fromBase64("5pel5pyf77ya") + header.value("date").toString());

    // 客户名称
    y += height;
    painter->drawText(x + 24 * mm, y, QByteArray::fromBase64("5a6i5oi35ZCN56ew77ya") + header.value("nick").toString());

    // 联系电话
    painter->drawText(x + 120 * mm, y, QByteArray::fromBase64("6IGU57O755S16K+d77ya") + header.value("mobile").toString());

    // 客户地址
    y += height;
    painter->drawText(x + 24 * mm, y, QByteArray::fromBase64("5a6i5oi35Zyw5Z2A77ya") + header.value("location").toString());

    y += height * 1.2;

    painter->setPen(Qt::DashLine);

    painter->drawLine(x, y, width * mm , y);

    return y;
}

qreal Print::printContent(qreal y, qreal mm, const QVariantList &list, QPainter *painter)
{
    QPrinter *printer = dynamic_cast<QPrinter *>(painter->device());

   QMarginsF margins = printer->pageLayout().margins();
   qreal width = printer->paperSize(QPrinter::Millimeter).width() - margins.left() - margins.right();

   qreal height = 9.0 * mm;

   qreal x  = margins.left() * mm;


   QFont font;
   font.setPointSize(11.0);
   painter->setFont(font);

    qreal col1 = width * mm  * 0.25;
    qreal col2 = width * mm  * 0.15;
    qreal col3 = width * mm  * 0.15;

    qreal col4 = width * mm  * 0.12;
    qreal col5 = width * mm  * 0.10;
    qreal col6 = width * mm  * 0.12;

    QTextOption option;
    option.setAlignment(Qt::AlignCenter| Qt::AlignVCenter);

    QRectF rectangle;
    rectangle.setX(x);
    rectangle.setY(y);
    rectangle.setWidth(col1);
    rectangle.setHeight(height);
    painter->drawText(rectangle, QByteArray::fromBase64("5ZCN56ew"), option); // 名称

    option.setAlignment(Qt::AlignCenter| Qt::AlignVCenter);
    rectangle.setX(col1);
    rectangle.setY(y);
    rectangle.setWidth(col2);
    rectangle.setHeight(height);
    painter->drawText(rectangle, QByteArray::fromBase64("5ZOB54mM"), option); // 品牌

    rectangle.setX(col1 + col2);
    rectangle.setY(y);
    rectangle.setWidth(col3);
    rectangle.setHeight(height);
    painter->drawText(rectangle, "分类", option); // 分类

    rectangle.setX(col1 + col2 + col3);
    rectangle.setY(y);
    rectangle.setWidth(col4);
    rectangle.setHeight(height);
    painter->drawText(rectangle, "价格", option); // 价格

    rectangle.setX(col1 + col2 + col3 + col4);
    rectangle.setY(y);
    rectangle.setWidth(col5);
    rectangle.setHeight(height);
    painter->drawText(rectangle, "数量", option); // 数量

    rectangle.setX(col1 + col2 + col3 + col4 + col5);
    rectangle.setY(y);
    rectangle.setWidth(col6);
    rectangle.setHeight(height);
    painter->drawText(rectangle, "金额", option); // 金额

    y += height * 1.2;

    painter->setPen(Qt::DashLine);

    painter->drawLine(x, y, width * mm , y);


    QListIterator<QVariant> itor(list);

    while (itor.hasNext()) {
        QVariantMap data = itor.next().toMap();

        // 名称
        rectangle.setX(x);
        rectangle.setY(y);
        rectangle.setWidth(col1);
        rectangle.setHeight(height);
        painter->drawText(rectangle, data.value("name").toString(), option);

        // 品牌
        rectangle.setX(col1);
        rectangle.setY(y);
        rectangle.setWidth(col2);
        rectangle.setHeight(height);
        painter->drawText(rectangle, data.value("brand").toString(), option);

        // 分类
        rectangle.setX(col1 + col2);
        rectangle.setY(y);
        rectangle.setWidth(col3);
        rectangle.setHeight(height);
        painter->drawText(rectangle, data.value("category").toString(), option);

        // 价格
        rectangle.setX(col1 + col2 + col3);
        rectangle.setY(y);
        rectangle.setWidth(col4);
        rectangle.setHeight(height);
        painter->drawText(rectangle, QString::number(data.value("price").toFloat(), 'f', 1) + "元", option);

        // 数量
        rectangle.setX(col1 + col2 + col3 + col4);
        rectangle.setY(y);
        rectangle.setWidth(col5);
        rectangle.setHeight(height);
        painter->drawText(rectangle, data.value("num").toString() + "件", option);

        // 金额
        rectangle.setX(col1 + col2 + col3 + col4 + col5);
        rectangle.setY(y);
        rectangle.setWidth(col6);
        rectangle.setHeight(height);
        painter->drawText(rectangle, QString::number(data.value("total").toFloat(), 'f', 1) + "元", option);

        y += height * 1.05; // 修正值

        painter->drawLine(x, y, width * mm, y);
    }

    return y;
}

void Print::printFooter(qreal y, qreal mm, QPainter *painter, const QVariantMap &footer)
{
     QPrinter *printer = dynamic_cast<QPrinter *>(painter->device());

   QMarginsF margins = printer->pageLayout().margins();

   qreal x  = margins.left() * mm;

   qreal height = 7.0 * mm;

   y += height;

   QFont font;
   font.setPointSize(11.0);

    font.setBold(true);
    painter->setFont(font);

    painter->drawText(QPointF(x + 120 * mm, y), ("合计：")); // 合计

    QString strNum = QString::number(footer.value("num").toInt());
    QString strMoney = QString::number(footer.value("total").toDouble(), 'f', 1);

    painter->drawText(QPointF(x + 132 * mm, y), strNum + " 件 共 " +  strMoney + " 元");
}

